package kr.co.bettersoft.waitnumberstorem.adapter;

import java.util.List;

import kr.co.bettersoft.waitnumberstorem.activity.R;
import kr.co.bettersoft.waitnumberstorem.common.CommonConstant;
import kr.co.bettersoft.waitnumberstorem.domain.WaitingRecommendation;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

public class MemberStoreReviewListAdapter extends BaseAdapter {
	private Context context;
	private final List<WaitingRecommendation> entries;
 
	// 폰트
	private Typeface typefaceNanumGothic= null; 
	private Typeface typefaceNanumGothicBold = null;
	
	public MemberStoreReviewListAdapter(Context context, List<WaitingRecommendation> entriesFn) {		
		this.context = context;
		this.entries = entriesFn;
		
		// 폰트 지정.
	    typefaceNanumGothic = Typeface.createFromAsset(context.getAssets(), CommonConstant.NanumGothic);  
	    typefaceNanumGothicBold = Typeface.createFromAsset(context.getAssets(), CommonConstant.NanumGothicBold);
	}
 
	public View getView(int position, View convertView, ViewGroup parent) {
 
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
		View gridView;
 
		if (convertView == null) {
 
			gridView = new View(context);
 
			// get layout from mobile.xml
			gridView = inflater.inflate(R.layout.member_store_review_list_adapter, null);
 
			// ** 이 소스를 여기에 두면 한 화면의 이미지가 반복 된다. 아래쪽 return 위로 옮겨주면 이미지 반복 현상이 없다. 
//			// set value into textview
//			TextView textView = (TextView) gridView
//					.findViewById(R.id.label);
//			textView.setText(((WaitingRecommendation)entries.get(position)).getCompanyName());
//			// set image based on selected text
//			ImageView imageView = (ImageView) gridView
//					.findViewById(R.id.icon);
////			String mobile = entries[position];
//			imageView.setImageBitmap(((WaitingRecommendation)entries.get(position)).getWaitingRecommendationImage());
		} else {
			gridView = (View) convertView;
		}
 
		// ** 이미지 반복 현상을 없애기 위해 위쪽의 소스를 여기로 옮겨준다. --> 문제 해결 됨.
		// set value into textview
		TextView customerNameView = (TextView) gridView.findViewById(R.id.customer_name);
		TextView registerDateView = (TextView) gridView.findViewById(R.id.register_date);
		RatingBar mRating = (RatingBar) gridView.findViewById(R.id.rating_bar);
		TextView reviewEvalValueView = (TextView) gridView.findViewById(R.id.review_eval_value);
		TextView reviewContentView = (TextView) gridView.findViewById(R.id.review_content);
		
		String tmpStr;
		
		// 작성자
		tmpStr = ((WaitingRecommendation)entries.get(position)).getMemberId();
		if(tmpStr!=null && tmpStr.length()>4){
			tmpStr = tmpStr.substring(tmpStr.length()-4);
		}
		customerNameView.setText(tmpStr);		// 가장 뒤 4글자만.
		// set image based on selected text
		
//		String mobile = entries[position];
		
		// 작성일
		registerDateView.setText(((WaitingRecommendation)entries.get(position)).getRegisterDate());
		
		// 별점, 평점
		tmpStr = ((WaitingRecommendation)entries.get(position)).getEvaluation();
		if(tmpStr==null || tmpStr.length()<1){
			tmpStr = "0.0";
		}
		mRating.setRating(Float.parseFloat(tmpStr)/2);		// 별점
		reviewEvalValueView.setText(tmpStr);				// 평점
		
		// 리뷰 내용
		reviewContentView.setText(((WaitingRecommendation)entries.get(position)).getContent());
		
		// 폰트 적용
		customerNameView.setTypeface(typefaceNanumGothic);		 
		reviewEvalValueView.setTypeface(typefaceNanumGothic);		 
	    reviewContentView.setTypeface(typefaceNanumGothic);		 
	    registerDateView.setTypeface(typefaceNanumGothic);	
	    
		return gridView;
	}
 
	@Override
	public int getCount() {
		return entries.size();
	}
 
	@Override
	public Object getItem(int position) {
		return null;
	}
 
	@Override
	public long getItemId(int position) {
		return 0;
	}
 
}